#include "FiveWin.ch"

static nId := 100

//----------------------------------------------------------------------------//

CLASS TControl FROM TWindow

   DATA   nHelpId
   DATA   bValid
   DATA   cCaption

   METHOD Change() VIRTUAL
   METHOD Click()  VIRTUAL
   METHOD cToChar( cCtrlClass )
   METHOD Init( hDlg )
   METHOD GetNewId() BLOCK { || ++nId }
   METHOD HandleEvent( nMsg, nWParam, nLParam )
   METHOD Valid()
   METHOD SetFocus( bOnOff ) BLOCK ;
                    { | Self, lOnOff | If( lOnOff, SetFocus( ::hWnd ),) }

ENDCLASS

//----------------------------------------------------------------------------//

METHOD cToChar( cCtrlClass ) CLASS TControl

   local cResult := ""

   DEFAULT cCtrlClass := ::ClassName(),;
           ::nId      := ::GetNewId(),;
           ::nStyle   := nOR( WS_CHILD, WS_VISIBLE, WS_TABSTOP )

   cResult += I2Bin( ::nLeft ) + I2Bin( ::nTop ) + ;
              I2Bin( ::nRight - ::nLeft + 1 ) + ;
              I2Bin( ::nBottom - ::nTop + 1 )
   cResult += I2Bin( ::nId )
   cResult += L2Bin( ::nStyle )
   cResult += cCtrlClass + B2Bin( 0 )
   cResult += ::cCaption + I2Bin( 0 )

return cResult

//----------------------------------------------------------------------------//

METHOD Init( hDlg ) CLASS TControl

   local oRect

   ::hWnd    = GetDlgItem( hDlg, ::nId )
   oRect     = ::GetRect()
   ::nTop    = oRect:nTop
   ::nLeft   = oRect:nLeft
   ::nBottom = oRect:nBottom
   ::nRight  = oRect:nRight

   ::Link()

   if ::oFont != nil
      ::SetFont( ::oFont )
   endif

return

//----------------------------------------------------------------------------//

METHOD Valid() CLASS TControl

   if ::bValid != nil
      return Eval( ::bValid )
   endif

return .t.

//----------------------------------------------------------------------------//

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TControl

   local nDlgCode

   do case
      case nMsg == WM_CLICK
           ::Click()

      case nMsg == WM_CHANGE
           ::Change()

      case nMsg == WM_DESTROY
           ::Release()

      case nMsg == WM_GETDLGCODE
           nDlgCode = ::GetDlgCode()
           if !empty( nDlgCode )
              return nDlgCode
           endif

      case nMsg == WM_KEYDOWN
           if nWParam == VK_F1
              HelpTopic( ::nHelpId )
           endif

      case nMsg == WM_KILLFOCUS
           ::SetFocus( .f. )

      case nMsg == WM_SETFOCUS
           ::SetFocus( .t. )

   endcase

return nil

//----------------------------------------------------------------------------//
